<?php
/**
 * Created by PhpStorm.
 * User: tsy
 * Date: 2016/6/15
 * Time: 15:00
 */
namespace Admin\Controller\Report;
use Think\Controller;

class PersonreportController extends Controller{
    /**
     * 个人基本信息报文table
     */
    public function person_report(){
        $month_time=date("Y-m",strtotime("-1 month",time()));
        $month_time = I("post.month_time",$month_time);
        $start_time = strtotime($month_time."-01");
        $end_time = strtotime("+1 month",$start_time)-1;
        $select_name=I("post.select_name","");
        $report=D("Report");
        $report_array=$report->get_person_info($start_time,$end_time,$select_name);
        $json=json_encode($report_array);
        $this->assign('report_json',$json);

        $search_array['month_time'] = $month_time;
        $search_array['select_name']=$select_name;
        $this->assign("search_array",$search_array);
        $this->display();
    }

    /**
     * 个人基本信息报文导出
     */
    public function person_report_export(){
        $month_time=date("Y-m",strtotime("-1 month",time()));
        $month_time = I("post.month_time",$month_time);
        $start_time = strtotime($month_time."-01");
        $end_time = strtotime("+1 month",$start_time)-1;
        $select_name=I("post.select_name","");
        $report=D("Report");
        $report_array=$report->get_person_info($start_time,$end_time,$select_name);
        $count=count($report_array);

        $person_basic=C("person_basic");
        $identity_info=C("identity_info");
        $profession_info=C("profession_info");
        $live_info=C("live_info");
        $org_code=C("org_code");

        $number_array=C("number_array");
        $flowing_water="";
        for($j=0;$j<3;$j++){
            $k=rand(0,36);
            $flowing_water.=$number_array[$k];
        }
        $dir=$org_code.date("Ymd",time()).$flowing_water."1";
        $file_name=$dir.".txt";
        $path="Public/file/".$file_name;
        $file=fopen($path,"a+");

        $identity_type=0;//身份证类型：0代表身份证
        for($i=0;$i<$count;$i++){
            $cust_name=$report_array[$i]['cust_name'];
            if($cust_name==''){
                continue;
            }
            $cust_name=report_switch($cust_name,30);
            $cust_identity=$report_array[$i]['cust_identity'];
            $cust_sex=$report_array[$i]['cust_sex'];
            $create_date=substr($cust_identity,6,8);
            $marry_status=str_pad($report_array[$i]['marry_status'],2," ",STR_PAD_RIGHT);
            $education_status=str_pad($report_array[$i]['education_status'],2," ",STR_PAD_RIGHT);
            $degree_status=str_pad($report_array[$i]['degree_status'],1," ",STR_PAD_RIGHT);
            $address_phone=str_pad("",25," ",STR_PAD_RIGHT);
            $cust_phone=str_pad($report_array[$i]['cust_phone'],16," ",STR_PAD_RIGHT);
            $company_phone=str_pad("",22," ",STR_PAD_RIGHT);
            $cust_email=str_pad($report_array[$i]['cust_email'],33," ",STR_PAD_RIGHT);
            $communication_address=report_address($report_array[$i]['communication_province'],$report_array[$i]['communication_city'],$report_array[$i]['communication_district'],$report_array[$i]['communication_address'],60);
            $communication_postcode=str_pad($report_array[$i]['communication_postcode'],6," ",STR_PAD_RIGHT);
            $identity_address=str_pad("",60," ",STR_PAD_RIGHT);
            $mate_name=str_pad("",30," ",STR_PAD_RIGHT);
            $mate_identity=str_pad("",18," ",STR_PAD_RIGHT);
            $mate_company=str_pad("",60," ",STR_PAD_RIGHT);
            $mate_phone=str_pad("",25," ",STR_PAD_RIGHT);

            $first_linkman_name=report_switch($report_array[$i]['first_linkman_name'],30);
            $first_linkman_type=str_pad($report_array[$i]['first_linkman_type'],1," ",STR_PAD_LEFT);
            $first_linkman_phone=substr($report_array[$i]['first_linkman_phone'],0,-4)."XXXX";
            $first_linkman_phone=$first_linkman_phone=="XXXX"?"":$first_linkman_phone;
            $first_linkman_phone=str_pad($first_linkman_phone,25," ",STR_PAD_RIGHT);
            $second_linkman_name=report_switch($report_array[$i]['second_linkman_name'],30);
            $second_linkman_type=str_pad($report_array[$i]['second_linkman_type'],1," ",STR_PAD_LEFT);
            $second_linkman_phone=substr($report_array[$i]['second_linkman_phone'],0,-4)."XXXX";
            $second_linkman_phone=$second_linkman_phone=="XXXX"?"":$second_linkman_phone;
            $second_linkman_phone=str_pad($second_linkman_phone,25," ",STR_PAD_RIGHT);

            $pos_code=str_pad($report_array[$i]['pos_code'],1," ",STR_PAD_RIGHT);
            $company_name=report_switch($report_array[$i]['company_name']==''?'暂缺':$report_array[$i]['company_name'],60);
            $industry_code=str_pad($report_array[$i]['industry_code'],1," ",STR_PAD_RIGHT);
            $company_address=report_address($report_array[$i]['company_province'],$report_array[$i]['company_city'],$report_array[$i]['company_district'],$report_array[$i]['company_address'],60);
            $company_postcode=str_pad($report_array[$i]['company_postcode'],6," ",STR_PAD_RIGHT);
            $company_start_time=str_pad("",4," ",STR_PAD_RIGHT);
            $du_code=str_pad($report_array[$i]['du_code'],1," ",STR_PAD_RIGHT);
            $ti_code=str_pad($report_array[$i]['ti_code'],1," ",STR_PAD_RIGHT);
            $year_income=str_pad("",10," ",STR_PAD_LEFT);

            $live_address=report_address($report_array[$i]['live_province'],$report_array[$i]['live_city'],$report_array[$i]['live_district'],$report_array[$i]['live_address'],60);
            $live_postcode=str_pad($report_array[$i]['live_postcode'],6," ",STR_PAD_RIGHT);
            $live_status=str_pad($report_array[$i]['li_code'],1," ",STR_PAD_RIGHT);

            $basic_string=$person_basic.$org_code.$cust_name.$identity_type.$cust_identity;
            $identity_string=$identity_info.$cust_sex.$create_date.$marry_status.$education_status.$degree_status.$address_phone.
                $cust_phone.$company_phone.$cust_email.$communication_address.$communication_postcode. $identity_address.$mate_name.
                " ".$mate_identity.$mate_company.$mate_phone.$first_linkman_name.$first_linkman_type.$first_linkman_phone.
                $second_linkman_name.$second_linkman_type.$second_linkman_phone;
            $profession_string=$profession_info.$pos_code.$company_name.$industry_code.$company_address.$company_postcode.
                $company_start_time.$du_code.$ti_code.$year_income;
            $live_string=$live_info.$live_address.$live_postcode.$live_status;

            $file_string=$basic_string.$identity_string.$profession_string.$live_string;

            fwrite($file,$file_string."\r\n");

        }
        $this->ajaxReturn($file_name);
    }


    public function person_unline(){
        $month_time=date("Y-m",strtotime("-1 month",time()));
        $month_time = I("post.month_time",$month_time);
        $start_time = strtotime($month_time."-01");
        $end_time = strtotime("+1 month",$start_time)-1;
        $cust_name=I("post.cust_name","");
        $report=D("Report");
        $report_array=$report->get_person_unline($start_time,$end_time,$cust_name);

        $json=json_encode($report_array);
        $this->assign('report_json',$json);

        $search_array['month_time']=$month_time;
        $search_array['cust_name']=$cust_name;
        $this->assign("search_array",$search_array);
        $this->display();
    }

    public function person_unline_insert(){
        $data=I("post.data");
        $report=D("Report");
        $data_count=count($data);
        for($i=0;$i<$data_count;$i++) {
            $id=$data[$i]['id'];
            $identity_array=$report->select_identity_person_unline($id);
            $identity_count=count($identity_array);
            if($identity_count!=0){
                $result=$report->update_person_unline($data[$i]);
            }else{
                $result=$report->insert_person_unline($data[$i]);
            }
        }
        $this->ajaxReturn($result);
    }

    public function person_unline_export(){
        $month_time=date("Y-m",strtotime("-1 month",time()));
        $month_time = I("post.month_time",$month_time);
        $start_time = $month_time."-01";
        $end_time = date("Y-m-d H:i:s",strtotime("+1 month",$start_time)-1);
        $cust_name=I("post.cust_name","");
        $report=D("Report");
        $report_array=$report->get_person_unline($start_time,$end_time,$cust_name);

        $count=count($report_array);

        $person_basic=C("person_basic");
        $identity_info=C("identity_info");
        $profession_info=C("profession_info");
        $live_info=C("live_info");
        $org_code=C("org_code");

        $number_array=C("number_array");
        $flowing_water="";
        for($j=0;$j<3;$j++){
            $k=rand(0,36);
            $flowing_water.=$number_array[$k];
        }
        $dir=$org_code.date("Ymd",time()).$flowing_water."1";
        $file_name=$dir.".txt";
        $path="Public/file/".$file_name;
        $file=fopen($path,"a+");


        for($i=0;$i<$count;$i++){
            $cust_name=$report_array[$i]['cust_name'];
            if($cust_name==''){
                continue;
            }
            $cust_name=report_switch($cust_name,30);
            $identity_type=str_pad($report_array[$i]['identity_type'],1," ",STR_PAD_RIGHT);//身份证类型：0代表身份证
            $cust_identity=str_pad($report_array[$i]['identity_number'],18," ",STR_PAD_RIGHT);
            $cust_sex=str_pad($report_array[$i]['cust_sex'],1," ",STR_PAD_RIGHT);
            $create_date=str_pad(substr($cust_identity,6,8),8," ",STR_PAD_RIGHT);
            $marry_status=str_pad($report_array[$i]['marry_status'],2," ",STR_PAD_RIGHT);
            $education_status=str_pad($report_array[$i]['education_status'],2," ",STR_PAD_RIGHT);
            $degree_status=str_pad($report_array[$i]['degree_status'],1," ",STR_PAD_RIGHT);
            $address_phone=str_pad($report_array[$i]['address_phone'],25," ",STR_PAD_RIGHT);
            $cust_phone=str_pad($report_array[$i]['telephone'],16," ",STR_PAD_RIGHT);
            $company_phone=str_pad($report_array[$i]['company_phone'],25," ",STR_PAD_RIGHT);
            $cust_email=str_pad($report_array[$i]['cust_email'],33," ",STR_PAD_RIGHT);
            $communication_address=report_switch($report_array[$i]['communication_address'],60);
            $communication_postcode=str_pad(substr($report_array[$i]['communication_postcode'],0,6),6," ",STR_PAD_RIGHT);
            $identity_address=report_switch($report_array[$i]['register_address'],60);
            $mate_name=report_switch($report_array[$i]['mate_name'],30);
            $mate_identity=str_pad($report_array[$i]['mate_identitytype'],18," ",STR_PAD_RIGHT);
            $mate_company=report_switch($report_array[$i]['mate_commpany'],60);
            $mate_phone=str_pad($report_array[$i]['mate_phone'],25," ",STR_PAD_RIGHT);

            $first_linkman_name=report_switch($report_array[$i]['first_linkman_name'],30);
            $first_linkman_type=str_pad($report_array[$i]['first_linkman_type'],1," ",STR_PAD_LEFT);
            $first_linkman_phone=substr($report_array[$i]['first_linkman_phone'],0,-4)."XXXX";
            $first_linkman_phone=$first_linkman_phone=="XXXX"?"":$first_linkman_phone;
            $first_linkman_phone=str_pad($first_linkman_phone,25," ",STR_PAD_RIGHT);
            $second_linkman_name=report_switch($report_array[$i]['second_linkman_name'],30);
            $second_linkman_type=str_pad($report_array[$i]['second_linkman_type'],1," ",STR_PAD_LEFT);
            $second_linkman_phone=substr($report_array[$i]['second_linkman_phone'],0,-4)."XXXX";
            $second_linkman_phone=="XXXX"?"":$second_linkman_phone;
            $second_linkman_phone=str_pad($second_linkman_phone,25," ",STR_PAD_RIGHT);

            $pos_code=str_pad($report_array[$i]['profession_status'],1," ",STR_PAD_RIGHT);
            $company_name=report_switch($report_array[$i]['company_name'],60);
            $industry_code=str_pad($report_array[$i]['industry_status'],1," ",STR_PAD_RIGHT);
            $company_address=report_switch($report_array[$i]['company_address'],60);
            $company_postcode=str_pad(substr($report_array[$i]['company_postcode'],0,6),6," ",STR_PAD_RIGHT);
            $company_start_time=str_pad($report_array[$i]['work_date'],4," ",STR_PAD_RIGHT);
            $du_code=str_pad($report_array[$i]['duty_status'],1," ",STR_PAD_RIGHT);
            $ti_code=str_pad($report_array[$i]['title_status'],1," ",STR_PAD_RIGHT);
            $year_income=str_pad($report_array[$i]['annual_income'],10," ",STR_PAD_LEFT);

            $live_address=report_switch($report_array[$i]['live_address'],60);
            $live_postcode=str_pad(substr($report_array[$i]['live_postcode'],0,6),6," ",STR_PAD_RIGHT);
            $live_status=str_pad($report_array[$i]['live_status'],1," ",STR_PAD_RIGHT);

            $basic_string=$person_basic.$org_code.$cust_name.$identity_type.$cust_identity;
            $identity_string=$identity_info.$cust_sex.$create_date.$marry_status.$education_status.$degree_status.$address_phone.
                $cust_phone.$company_phone.$cust_email.$communication_address.$communication_postcode. $identity_address.$mate_name.
                $identity_type.$mate_identity.$mate_company.$mate_phone.$first_linkman_name.$first_linkman_type.$first_linkman_phone.
                $second_linkman_name.$second_linkman_type.$second_linkman_phone;
            $profession_string=$profession_info.$pos_code.$company_name.$industry_code.$company_address.$company_postcode.
                $company_start_time.$du_code.$ti_code.$year_income;
            $live_string=$live_info.$live_address.$live_postcode.$live_status;

            $file_string=$basic_string.$identity_string.$profession_string.$live_string;

            fwrite($file,$file_string."\r\n");
        }
        $this->ajaxReturn($file_name);
    }

}